package com.bunkeystudio;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class Rangefinder extends Activity {
	
	private Viewfinder	_viewfinder;
	private Camera		_camera;
	private int			_defaultCamera;
	private Button		_shutter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
      
        _viewfinder = new Viewfinder(this);
        //setContentView(R.layout.main);
        _shutter = new Button(this);
        _shutter.setText("Shutter");

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        		ViewGroup.LayoutParams.WRAP_CONTENT, 
        		ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        _shutter.setLayoutParams(params);
        
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
        		ViewGroup.LayoutParams.FILL_PARENT));
        layout.addView(_shutter);
        
        FrameLayout frame = new FrameLayout(this);
        frame.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
        		ViewGroup.LayoutParams.MATCH_PARENT));
        
        frame.addView(_viewfinder);
        frame.addView(layout);
        
        setContentView(frame);
        /*try{
        	File f = new File(Environment.getExternalStorageDirectory(), "hello.txt");
        	FileOutputStream fos = new FileOutputStream(f);
        	OutputStreamWriter osw = new OutputStreamWriter(fos);
        	osw.write("hello world!\n");
        	osw.flush();
        	osw.close();
        }catch(Exception e){
        	
        }*/
    }
    
    protected void onResume(){
    	super.onResume();
    	
    	_camera = Camera.open();
    	_viewfinder.setCamera(_camera);
    	
    	_shutter.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		//_camera = _viewfinder.getCamera();
        		_camera.takePicture(null, null, new PictureCallback(){
        			@Override
            		public void onPictureTaken(byte[] data, Camera camera) {
            			Log.d("rangefinder", "jpeg");
            			if(data != null){
            				try{
            					//BitmapFactory.Options options = new BitmapFactory.Options();
            					Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
            					File f = new File(Environment.getExternalStorageDirectory(), "photo.JPG");
            					FileOutputStream fos = new FileOutputStream(f);
            					BufferedOutputStream bos = new BufferedOutputStream(fos);
            					image.compress(CompressFormat.JPEG, 100, bos);
            					bos.flush();
            					bos.close();
            					
            				}catch(Exception e){
            					Log.e("error", e.toString());
            				}
            			}
            		}
        		});
        	}
        });  
    	
    }
    
    protected void onPause(){
    	super.onPause();
    	
    	if(_camera != null){
    		_viewfinder.setCamera(null);
    		_camera.release();
    		_camera = null;
    	}
    }
}